#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Song {
    char name[100];
    double duration;
    int score;
};

struct ListeningRecord {
    char songName[100];
    double timeListened;
};

int compare(const void *a, const void *b) {
    struct Song *song1 = (struct Song *)a;
    struct Song *song2 = (struct Song *)b;

    if (song1->score != song2->score) {
        return song2->score - song1->score;
    }
    return strcmp(song1->name, song2->name);
}

int main() {
    int numSongs;
    while (scanf("%d", &numSongs) != EOF) {
        if (numSongs == 0)
            break;
        struct Song songs[101];
        char temp[10];
        memset(temp, '\0', sizeof(temp));

        for (int i = 0; i < numSongs; i++) {
            scanf("%s %s", songs[i].name, temp);
            songs[i].duration = ((temp[0] - '0') * 10 + (temp[1] - '0')) * 60 + ((temp[3] - '0') * 10 + (temp[4] - '0'));
            songs[i].score = 0;
        }

        int numListeningRecords;
        scanf("%d", &numListeningRecords);
        struct ListeningRecord records[10005];

        for (int i = 0; i < numListeningRecords; i++) {
            scanf("%s %s", records[i].songName, temp);
            records[i].timeListened = ((temp[0] - '0') * 10 + (temp[1] - '0')) * 60 + ((temp[3] - '0') * 10 + (temp[4] - '0'));
        }

        for (int i = 0; i < numListeningRecords; i++) {
            for (int j = 0; j < numSongs; j++) {
                if (strcmp(songs[j].name, records[i].songName) == 0) {
                    if (songs[j].duration == records[i].timeListened)
                        songs[j].score += 5;
                    else if (records[i].timeListened >= songs[j].duration / 5 * 4  && records[i].timeListened < songs[j].duration)
                        songs[j].score += 4;
                    else if (records[i].timeListened >= songs[j].duration / 5 * 3  && records[i].timeListened < songs[j].duration / 5 * 4 + 1)
                        songs[j].score += 3;
                    else if (records[i].timeListened >= songs[j].duration / 5 * 2  && records[i].timeListened < songs[j].duration / 5 * 3 + 1)
                        songs[j].score += 2;
                    else if (records[i].timeListened >= songs[j].duration / 5 && records[i].timeListened < songs[j].duration / 5 * 2 + 1)
                        songs[j].score += 1;
                    break;
                }
            }
        }
        
        qsort(songs, numSongs, sizeof(struct Song), compare);
        
        for (int i = 0; i < numSongs; i++) {
            printf("%s %d\n", songs[i].name, songs[i].score);
        }
    }
}
